package com.eight.clienthome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eight.clienthome.dao.ZoneSubjectDao;
import com.eight.clienthome.service.ZoneService;
import com.eight.clienthome.service.ZoneSubjectService;
import com.eight.common.pojo.TbBottomArticle;
import com.eight.common.pojo.TbZone;
import com.eight.common.pojo.ZoneSubject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 向文豪
 * @program: eight-education-parent
 * @description:
 * @date 2023/6/2 19:21
 */
@Service
public class ZoneSubjectServiceImpl implements ZoneSubjectService {
    @Autowired
    private ZoneSubjectDao zoneSubjectDao;

    @Override
    public List<ZoneSubject> findAll() {

        QueryWrapper<ZoneSubject> wrapper = new QueryWrapper<>();
        List<ZoneSubject> zoneSubjects = zoneSubjectDao.selectList(null);
        wrapper.eq("parent_id",0);
        List<ZoneSubject> zoneSubjects1 = zoneSubjectDao.selectList(wrapper);
        for (ZoneSubject zoneSubject : zoneSubjects1) {
            List<ZoneSubject> children = findChildren(zoneSubject, zoneSubjects);
            zoneSubject.setChildren(children);
        }
        return zoneSubjects1;
    }




    private List<ZoneSubject> findChildren(ZoneSubject item, List<ZoneSubject> childrens){
        ArrayList<ZoneSubject> list = new ArrayList<>();
        for (ZoneSubject children : childrens) {
            if(item.getId().equals(children.getParentId())){
                list.add(children);
            }
        }
        for (ZoneSubject eduSubject : list) {
            eduSubject.setChildren(findChildren(eduSubject,childrens));
        }
        return list;
    }


}
